home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / egebook.lha / ege.book / 3 / virtualbase.C < prev   
C/C++ Source or Header  |  1992-06-05  |  2KB  |  89 lines

  1. #include <stdio.h>
  2. #include "bool.h"
  3.  
  4. // Class definition for Person
  5.  
  6. class Person {
  7.   char* name;
  8. public:
  9.   Person();                // constructor
  10.   Person(char *n);         //    bodies declared elsewhere
  11.   void print();            // member function
  12. };
  13.  
  14. // Body for class Person
  15.  
  16.  Person::Person(){         // constructor
  17.    name = "";
  18.  }
  19.  Person::Person(char *n){  // constructor
  20.    name = n;
  21.  };
  22.  void Person::print(){     // member functions
  23.    printf("my name is: %s \n", name);
  24.  };
  25.  
  26. // Class definition for Teacher
  27.                                 // Teacher is derived 
  28. class Teacher: public Person {  //        from Person
  29.   int courses;
  30.   static int MaxCourses;
  31. public:
  32.   Teacher(char *name):Person(name){
  33.     courses = 0;
  34.   }
  35.   bool check();
  36.   void addCourse();
  37. };
  38.  
  39. // Body for class Teacher
  40.  
  41. int Teacher::MaxCourses = 2;
  42.  
  43. bool Teacher::check(){
  44.   return (bool) (courses < MaxCourses);
  45. }
  46. void Teacher::addCourse() {
  47.   if (check()) 
  48.     courses++; 
  49. }
  50.  
  51. class Researcher: public Person {
  52.   char *expertise;          // field of expertise
  53. public:
  54.   Researcher(char *e, char *n):Person(n){      // constructor
  55.     expertise = e;
  56.   }
  57.   void print(){
  58.     printf("I am an expert in: %s\n", expertise);
  59.   }
  60. };
  61.  
  62. class Professor: public Teacher, public Researcher {
  63.   enum nature {teacher, researcher} kind;
  64. public:
  65.                           // combined constructor
  66.   Professor(char *name, char* exp):
  67.       Teacher(name),Researcher(exp,name){};
  68.  
  69.   void change(){          // change nature
  70.     if (kind == teacher)
  71.        kind = researcher;
  72.     else
  73.        kind = teacher;
  74.   }
  75.   void print(){           // print according to nature
  76.     Teacher::print();
  77.     if (kind == researcher) {
  78.        printf("and ");
  79.        Researcher::print();
  80.     }
  81.   }
  82. };
  83.  
  84. main(){
  85.    Professor p("John Prof","something");
  86.  
  87.    p.print();
  88. }
  89.